import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useUserinfoStore = defineStore('userinfo', () => {
    const userinfo = ref({
        userid: '', // 用户唯一id
        username: '', // 账号
        realname: '', // 真实姓名
        sex: '', // 性别
    })

    if (sessionStorage.getItem('user')) {
        let user = JSON.parse(sessionStorage.getItem('user'))
        setUserinfo(user)
    }
    function setUserinfo(user) {
        userinfo.value = user
        userinfo.value.sex = (user.sex === 0 ? '男': '女')
    }

    const getUserinfo = computed(() => {
        return userinfo.value
    })

    return { userinfo, setUserinfo, getUserinfo }
})
